﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApiV3.Data.Entity;
using WebApiV3.Domain;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebApiV3.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UsersController : ControllerBase
    {
        private readonly IResponsitory<Users> _userRespository;

        public UsersController(IResponsitory<Users> userRespository)
        {
            _userRespository = userRespository;
        }


        // GET: api/<UsersController>
        [HttpGet]
        public IEnumerable<Users> Get()
        {
            return _userRespository.Table.ToList();
        }

        // GET api/<UsersController>/5
        [HttpGet("{id}")]
        public Users Get(int id)
        {
            return _userRespository.GetById(id);
        }

        // POST api/<UsersController>
        [HttpPost]
        public void Post([FromBody] Users value)
        {
            _userRespository.Insert(value);
        }

        // PUT api/<UsersController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] Users value)
        {
            var user=_userRespository.GetById(id);

            if (user != null)
            {
                user.UserName = value.UserName;
                user.Password = value.Password;
                user.UpdateTime = DateTime.Now;

                _userRespository.Update(user);
            }
            
        }

        // DELETE api/<UsersController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
            _userRespository.Delete(id);
        }
    }
}
